<?php
namespace Attendance\Controller;
use Common\Controller\AdminbaseController;
use Home\Service\ClockTimeService;
use Home\Service\AccountService;
// +----------------------------------------------------------------------
// | Date:2017年2月15日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class EmployeeController extends AdminbaseController{
    
    public function _initialize(){
        parent::_initialize();
        $this->model = D('AttendanceDay');

    }
    
    public function showrule($n){

        switch ($n){
            case 1:
                   $showtxt = [
                                '实际工时 = 打卡时长+(外出时间/长途时间)',
                            ];
                   break;
            case 2://应出勤天数
                   $showtxt = [
                                '应出勤天数和员工的入职日期(离职日期)密切相关',
                                '如果员工的离职日期是1月19日，那么该员工的应出勤天数会从月初计算到19日为止',
                                '入职日期也是如此',
                            ];
                   break;
            case 3://缺时
                   $showtxt = [
                                '缺时=应出勤时长 - 当月的假期申请时长（事假除外）- 旷工时长',
                            ];
                   break;
            case 4://准时在岗率
                   $showtxt = [
                                '准时在岗率=9点到18点打卡的时长/（应出勤时长-全部请假时长）',
                            ];
                   break;
            
        }
        
        foreach ($showtxt as $k=>$v) {
            $showhtml .= "<li>{$v}</li>";
        }   
        
        echo "<ul>{$showhtml}</ul>";
                
    }
    
    
    /**
     * 日数据报表
     * 
     * date:2017年3月8日
     * author: EK_熊
     */
    public function index(){
        $start_time = I('start_time');
        $end_time = I('end_time');
        $name = I('name');
        $dep_id = I('dep_id');
        if ($start_time) $where['date'] = $start_time;
        if ($start_time && $end_time) $where['date'] = ['between',["$start_time 00:00:00","$end_time 23:59:59"]];
        if ($name) $where['name'] = ['like',"%{$name}%"];
        if($dep_id) {
            //先获取是否有子节点
            $account = new AccountService();
            $dep_child_id = $account->get_department_child_id($dep_id);
            if (count($dep_child_id) > 1){
                $where['dep_id'] = ['in',$dep_child_id];
            }else{
                $where['dep_id'] = $dep_child_id[0];
            }
        }
        
        
        $list = $this->lists($this->model,$where,'date DESC',$field='*');
        $list = $this->_index_data_filter($list);
        
        if ($where) $this->assign('map',urlencode(json_encode($where)));
        $this->assign('list',$list);
        $department = D('Home/Account','Service')->get_department_select($dep_id);
        $this->assign('department',$department);
        $all_leave = $this->get_all_leave();
        $this->assign('allleave',$all_leave);
        $this->display();
    }
    //获取全部假期标签和中文
    public function get_all_leave(){
        $leave_en = C('VACATION_TYPE_EN');
        $leave_zh = C('VACATION_TYPE');
        $leaveBox = [];
        foreach ($leave_en as $k=>$v){
            $leaveBox[$v] = $leave_zh[$k];
        }
        $leaveBox['leave_evection']='短途外出';
        $leaveBox['leave_long_distance']='长途出差';
        return $leaveBox;
    }
    
    public function print_index(){
        $where = json_decode(urldecode(I('map')),true);
        if (empty($where)) {
           $this->error('请输入查询条件!!');
        }
        //获取全部假期 
        $all_leave = $this->get_all_leave();
        $this->assign('allleave',$all_leave);
        if (!isset($where['name']) && !isset($where['dep_id'])) $this->error('缺少查询条件【姓名】或者【部门】');
        $list = $this->model->where($where)->order('date DESC')->select();
        $list = $this->_index_data_filter($list);
        
        $name = trim($where['name'][1],'%');
        $this->assign('list',$list);
        $this->show_print('index',"考勤明细表-$name");
    }
    
    private function _index_data_filter($list){
        if ($list){
            foreach ($list as $k=>$v){
                $leavetime = leave_mins_day($v);
                
                if ($leavetime>0){
                    $leave_actu = $leavetime+intval($v['work_actual']);
                    if ($leave_actu < 480){
                        $list[$k]['stu_absent'] = 480-$leave_actu;
                        $stu_leave_early = $list[$k]['stu_absent'] - $v['stu_late'];
                        $list[$k]['stu_leave_early'] = $stu_leave_early >0 ? $stu_leave_early : 0;
                    }else{
                        $list[$k]['stu_leave_early'] = 0;
                        $list[$k]['stu_absent'] =0;
                    }
                }
            }
        }
        return $list;
    }
    
    /**
     * 月度数据查询
     * 月应出勤时数=	（当月天数-周末-法定节假日）*8			
                        月缺勤分钟数=	（月应出勤时数 - 月所有请假时间 - 月实际工时）* 60  			月请假时间（排除了病假和事假的其他全部假期）
                        月加班分钟数=	每天累加值			
                        月有效工时=	每天累加值			
                        月实际工时=	每天实际累加值+月出勤数			
                        月超时分钟=	每天超时累加值-月缺勤分钟数			负数不显示

     * date:2017年3月3日
     * author: EK_熊
     */
    public function monthly(){
        $start_date = I('start_date');
        $end_date = I('end_date');
        $period_start = date('Y-m-01'); //月第一天
        $period_end = date('Y-m-d',strtotime("-1 day"));//前一天
        $dep_id = I('dep_id');
        $name = I('name');
        $print = I('print');
        $filter = I('filter');
        if($name) $where['name']=['like',"%{$name}%"];
        if($dep_id) {
            //先获取是否有子节点
            $account = new AccountService();
            $dep_child_id = $account->get_department_child_id($dep_id);
            if (count($dep_child_id) > 1){
                $where['dep_id'] = ['in',$dep_child_id];
            }else{
                $where['dep_id'] = $dep_child_id[0];
            }
        }
        if($filter == 1) {
            $wx_map['is_attendance'] = 0;
            unset($wx_map['wx_openid']);
        }
        
        if ($filter == 2){
            unset($where['sn']);
        }
        if ($start_date && $end_date) {
            $period_start = $start_date;
            $period_end = $end_date;
        }
        if ($name) $where['name'] = ['like',"%{$name}%"];
        $where['date'] = ['between',[$period_start,$period_end]];
        $attenMon = $this->_monthly_data($where);
        $this->assign('list',$attenMon);
        $department = D('Home/Account','Service')->get_department_select($dep_id);
        $this->assign('department',$department);
        $this->display();
    }
    
    /**
     * 汇总表
     * @param unknown $where
     * @param string $ispage
     * date:2017年3月30日
     * author: EK_熊
     */
    private function _monthly_data($where,$ispage=true){
        $period_start = $where['date'][1][0];
        $period_end = $where['date'][1][1];
        
        $sql_leave_type_total = sql_leave_type(true);
        $leave_type = C('VACATION_TYPE_EN');
        $leave_type[] = 'leave_evection';
        $leave_type[] = 'leave_long_distance';
        foreach ($leave_type as $type){
            $sql_leave_type_sum .= ", sum($type) as $type";
        }

        $field = "name,dep_name,sn,dep_id,date,
            sum(work_effect) as work_effect,
            sum(overtime) as overtime,
            sum(work_actual) as work_actual,
            sum(work_effect_918) as work_effect_918,".$sql_leave_type_total.$sql_leave_type_sum;
        $account = new AccountService();
        
        if (!isset($where['name'])){
            $where['name']=['neq',''];//没姓名不显示
            
        }
        if (!$ispage) {
            $attenMon = $this->model->group('sn')->order($order)->where($where)->field($field)->select();
        }else{
            $total = $this->model->where($where)->count('DISTINCT sn');
            $page = $this->page($total, 20,1,$REQUEST['r']);
            $attenMon = $this->model->group('sn')->order($order)->where($where)->field($field)->limit($page->firstRow.','.$page->listRows)->select();
        }
        //计算可补休时间，从查询时间的结束点往前推60天
        $end = $where['date'][1][1];
        $emain_overtime_all = $this->model->count_remain_overtime_all($where['dep_id'],$end);

        if ($attenMon){
            $attenMon = $account->append_user_field('sn',$attenMon,'sn,ep_name,is_attendance,dep_id,dep_name,quit_date');
            foreach($attenMon as $k=>$v){
                if ($v['quit_date'] && ($v['quit_date'] < $period_start)) {
                    continue;
                }
                $teamAry = explode('/',$v['dep_name']);
                if ($teamAry) {
                    $attenMon[$k]['team_name'] = $teamAry[0];
                }
                $workday = work_days_in_interval($period_start, $period_end,$v['sn']);//应出勤时间数
                $workdayMin = $workday['min'];
                //考勤周期
                $attenMon[$k]['mon_start'] = $workday['start'];
                $attenMon[$k]['mon_end'] = $workday['end'];

                //旷工次数 = 无记录次数+缺时时长大于等于3小时的天数(无请假状态)
                $absentInfo = $this->model->_absenteeism_num_no_data($v['sn'],$period_start, $period_end);

                $attenMon[$k]['absenteeism_min'] = $absentInfo['min']; //旷工时长
                $attenMon[$k]['absenteeism_num'] = $absentInfo['num'];//旷工次数
                
                //缺勤数 = 应出勤时间（排除入职离职时间）-全部假期时间-实际出勤-旷工时间
                $stu_absent = $workdayMin - $v['total_leave']-$attenMon[$k]['work_actual']-$attenMon[$k]['absenteeism_min'];//$v['total_leave']已经包含了外出，所以不用再减去外出
  
                $attenMon[$k]['stu_absent'] = $stu_absent>0 ? $stu_absent : 0;
                $attenMon[$k]['stu_absent_rate'] = (round($attenMon[$k]['stu_absent']/$workdayMin,2)*100).'%';//$v['total_leave']已经包含了外出，所以不用再减去外出
                
                //实际出勤率 = 打卡时间+带薪假期（事假，病假半日薪，病假最低薪,产假（这个假不稳定，基本都是手工调整）除外的全部假期）+(外出，出差)
                //这里不需要累加哺乳假的时间，因为应出勤时间那边减好一个小时的计算
                $work_actual_leave = leave_mins_day_except($v,['leave_casual','leave_sick_half','leave_sick_min','leave_antenatal']);
                $work_actual = $v['work_actual']+$work_actual_leave;
                
                //实际工时,含周末,不能累加外出时间，已经计算到实际里面了
                $attenMon[$k]['work_actual'] = $v['work_actual'];
                $attenMon[$k]['work_day'] = $workday['day'];//应出勤天数
                $attenMon[$k]['work_hour'] = round($workdayMin/60);//应出勤小时数
                
                
              //  $attenMon[$k]['work_actual_rate_w'] = (round($attenMon[$k]['work_actual']/$workdayMin,2)*100).'%';//实际出勤率 = 打卡时间+带薪假期（事假，病假半日薪，病假最低薪除外的全部假期）
                $attenMon[$k]['work_actual_rate_w'] = (round($work_actual/$workdayMin,2)*100).'%';//实际出勤率 = 打卡时间+带薪假期（事假，病假半日薪，病假最低薪除外的全部假期）
                $attenMon[$k]['work_effect_rate_w'] = (round($v['work_effect']/$workdayMin,2)*100).'%';//有效出勤率
                $attenMon[$k]['worl_actual_day']=ceil($v['work_actual']/480);
                if ($attenMon[$k]['worl_actual_day'] > $attenMon[$k]['work_day']) $attenMon[$k]['worl_actual_day'] = $attenMon[$k]['work_day'];
                //实际出勤率,不含周末
                $workRate = $this->_rate_work($v['sn'], $workdayMin, $period_start, $period_end);
                $attenMon[$k]['work_actual_rate'] =$workRate['actual_rate'];
                $attenMon[$k]['work_effect_rate'] = $workRate['effect_rate'];
                $attenMon[$k]['work_effect_918_rate'] = $workRate['effect_918_rate'];
               
                //可补休时间
                $attenMon[$k]['remain_overtime'] = get_hour($emain_overtime_all[$v['sn']]);
                
                
                //如果只是单独导出全部免打卡的话，就不要做虚伪的过滤
                if ($v['is_attendance'] == 0 && I('print')){

                    $attenMon[$k]['stu_absent'] = 0;
                    $attenMon[$k]['stu_absent_rate'] = 0;
                    $attenMon[$k]['work_actual_rate'] = '100%';
                    $attenMon[$k]['work_effect_rate'] = '100%';
                    $attenMon[$k]['work_actual_rate_w'] = '100%';
                    $attenMon[$k]['work_effect_rate_w'] = '100%';
                    $attenMon[$k]['work_effect_918_rate'] = '100%';
                    
                    $attenMon[$k]['stu_late'] = 0;
                    $attenMon[$k]['stu_leave_early'] = 0;
                    $attenMon[$k]['work_effect'] = $workdayMin;
                    $attenMon[$k]['work_actual'] = $workdayMin;
                    $attenMon[$k]['work_effect_918'] = $workdayMin;
                    $attenMon[$k]['work_effect_918_rate'] = '100%';
                    $attenMon[$k]['work_effect_918_rate'] = '100%';
                    $attenMon[$k]['absenteeism_num']=0;
                    $attenMon[$k]['absenteeism_min']=0;
                    
                    
                    
                }
                
            }  
            

        }
        
        if ($ispage){
            $p =$page->show('Admin');
            $this->assign('page', $p? $p: '');
            $this->assign('total',$total);
        }
        unset($where['sn']);//要注销，不然查询工号太多，导致请求的链接太长，apache不支持
        if ($where) $this->assign('map',urlencode(json_encode($where)));

        if ($period_end < $period_start) {
            $period_end = $period_start;//跨月份
        }
        $this->assign('work_days',$workday);
        $this->assign('start_mon',$period_start);
        $this->assign('end_mon',$period_end);
        return $attenMon;
    }
    
    
    /**
     * 获取旷工时长，次数
     * 1.无记录情况
     * 2.(缺时时长 - 全部请假时长) >= 3小时
     * 3.9到18点 出勤数不满300分钟
     * 
     * date:2018年1月15日
     * author: EK_熊
     */
    private function _absenteeism_info($start,$end,$sn){
        $where['date'] = ['between',[$start,$end]];
        $where['sn'] = $sn;
        $work_date = D('AttendanceDay')->where($where)->getField('date,',true);
    }
    
    //获取旷工次数,无记录的情况
//     private function _absenteeism_num_no_data($start,$end,$sn){
//         $abs_num = 0;
//         $abs_num_total = 0; //一整天无记录
//         $abs_min = 0; //缺勤时长，这里是记录缺时》=3小时的缺时时长
        
//         $where['date'] = ['between',[$start,$end]];
//         $where['sn'] = $sn;
//         $leave_type = C('VACATION_TYPE_EN');
//         $leave_type[] = 'leave_evection';
//         $leave_type[] = 'leave_long_distance';
//         foreach ($leave_type as $type){
//             $sql_leave_type .= ", $type";
//         } 
//         $attInfo= D('AttendanceDay')->where($where)->field('date,stu_absent,work_effect_918'.$sql_leave_type)->select();
//         foreach ($attInfo as $k=>$v) {
//             $work_date[] = $v['date'];
//             $attInfo[$k]['total_leav'] = 0;
//             foreach ($v as $jkey=>$jval) {
//                 if (in_array_case($jkey, $leave_type)){
//                     $attInfo[$k]['total_leave'] += $jval;
//                 }
//             }
// //             if ((intval($v['stu_absent']) - $attInfo[$k]['total_leave']) >= 3*60){
// //                 $abs_num++;
// //                 $abs_min += $v['stu_absent'];
// //             }
            
//             if (intval($v['work_effect_918'] + $attInfo[$k]['total_leave'] <= 300) && is_work_day($v['date'])){
//                 $abs_num++;
//                 $abs_min += 480-$v['work_effect_918']-$attInfo[$k]['total_leave'];
//             }
//         }
//         $start_stmp = strtotime($start);
//         $end_stmp = strtotime($end);
        
//         $accountService = new AccountService();
//         $userinfo = $accountService->get_user_by_field(['sn'=>$sn],'join_date,quit_date');
        
//         //入职时间
//         if ($userinfo['join_date'] >=$start &&  $userinfo['join_date'] <= $end) {
//             $start_stmp = strtotime($userinfo['join_date']);
//         }
//         //离职时间
//         if ($userinfo['quit_date'] >=$start &&  $userinfo['quit_date'] <= $end) {
//             $end_stmp = strtotime($userinfo['quit_date']);
//         }
//         for($day=$start_stmp;$day<=$end_stmp;$day=$day+86400){
//             $day_str = date('Y-m-d',$day);
//             if (!in_array_case($day_str, $work_date) && is_work_day($day_str)){
//                 $abs_num_total++;
//             }
//         }
//         $ret['num'] = $abs_num_total+$abs_num;
//         $ret['min'] = $abs_num_total*480 + $abs_min;
//         return $ret;
//     }
    
    /**
     * 根据工号，获取用户的实际工时(不包含外出时间)，和有效工时，计算效率值，（不含非工作日）
     * @param unknown $sn
     * date:2017年4月5日
     * author: EK_熊
     */
    private function _rate_work($sn,$totalMis,$day_s,$day_e){
        $map['sn'] = $sn;
        $map['date'] = ['between',["$day_s 00:00:00","$day_e 23:59:59"]];
        
        $attInfo = D('AttendanceDay')->where($map)->select();
        $actual = 0;
        $effect = 0;
        foreach ($attInfo as $k=>$v){
            if (is_work_day($v['date'])){
                $actual += $v['work_actual'];
                $effect += $v['work_effect'];
                $effect918 += $v['work_effect_918'];
            }
        }
        
        $actual_rate = (round($actual/$totalMis,2)*100).'%';
        $effect_rate = (round($effect/$totalMis,2)*100).'%';
        $effect918 = (round($effect918/$totalMis,2)*100).'%';
        
        return ['actual_rate'=>$actual_rate,'effect_rate'=>$effect_rate,'effect_918_rate'=>$effect918];
        
    }
    
    /**
     * 处理月度报告数据的独立方法
     * @param unknown $where
     * @param unknown $field
     * date:2017年3月27日
     * author: EK_熊
     */
    public function print_monthly(){
        $where = json_decode(urldecode(I('map')),true);
        $attenMon = $this->_monthly_data($where,false);
            //获取全部部门数据
            $accountService = new AccountService();
            $alldep = $accountService->get_department();
            foreach($attenMon as $k=>$v){
                for ($i=0;$i<count($alldep);$i++) {
                    $dep_name = $alldep[$i]['dep_name'];
                    if ($v['dep_id'] == $alldep[$i]['dep_id']) {
                        $list[$dep_name][] = $v;
                    }
                }
            }
        $this->assign('list',$list);
        $this->assign('alldep',$alldep);  
        
        $this->show_print('monthly','考勤汇总表');
    }
    
    /**
     * 导出excel，不分部门
     * 
     * date:2017年5月27日
     * author: EK_熊
     */
    public function print_monthly_no_dep(){
        $where = json_decode(urldecode(I('map')),true);
        $attenMon = $this->_monthly_data($where,false);
        $this->assign('list',$attenMon);
        $this->show_print('monthly','考勤汇总表(不分部门)','no_dep');
    }
    
//     public function print_monthly_no_dep(){
//         $data = I('data');
//         if (!$data['date_start'] || !$data['date_end']) $this->error('请输入时间范围');
//         $dep_id = $data['dep_id'];
//         $key_depid = 'monnodep_depid'.$data['code'];
//         $key_monthly = 'monnodep_'.$data['code'];
//         if($dep_id) {
//             $session_depid = session($key_depid);
//             if ($session_depid){
//                 $where['dep_id'] = $session_depid;
//             }else{
//                 //先获取是否有子节点
//                 $account = new AccountService();
//                 $dep_child_id = $account->get_department_child_id($dep_id);
//                 if (count($dep_child_id) > 1){
//                     $where['dep_id'] = ['in',$dep_child_id];
//                 }else{
//                     $where['dep_id'] = $dep_child_id[0];
//                 }
//                 session($key_depid,$where['dep_id']);
//             }
//         }
        
        
//         if ($data['name']) $where['name'] = ['like',"%{$data['name']}%"];
//         $where['date'] = ['between',[$data['date_start'],$data['date_end']]];
//         $attenMon = $this->_monthly_data($where,false);
//         $session_monthly = session($key_monthly);
//         if ($session_monthly) {
//             $attenMon = array_merge($session_monthly,$attenMon);
//         }
//         session($key_monthly,$attenMon);
//         dump($attenMon);
// //         if ($end){
            
// //         }
// //         $this->assign('list',$attenMon);
// //         $this->show_print('monthly','考勤汇总表(不分部门)','no_dep');        
//     }
    

    /**
     * 加班报销单，不分组
     * 
     * date:2017年4月7日
     * author: EK_熊
     */
    public function overtime_form_all(){
        $ispage = I('print') ? false : true;
        $listdb = $this->_overtime_data($ispage);
        foreach ($listdb as $k=>$v){
            $listdb[$k]['meal_fee'] = $this->meal_fee($v['duty_on'], $v['duty_off'],$v['overtime']);
            $listdb[$k]['is_work_day'] = is_work_day($v['duty_on']) ? '工作日' : '非工作日';
            $listdb[$k]['duty_on'] =  $v['duty_on'] ? $v['duty_on'] : $v['date']." 09:00:00";
            $listdb[$k]['duty_off'] = $v['duty_off'] ? $v['duty_off'] : $v['date']." 18:00:00";
        }
        $this->assign('list',$listdb);        
        $this->show_print(ACTION_NAME,'加班报销单(不分组)');
        $this->display();
    }
    
    
    /**
     * 加班数据集合，包含打印预览和打印的功能区分
     * 
     * date:2017年4月7日
     * author: EK_熊
     */
    private function _overtime_data($page=false){
        $start_date = I('start_date');
        $end_date = I('end_date');
        $period_start = date('Y-m-01'); //月第一天
        $period_end = date('Y-m-d',strtotime("$period_start +1 month -1 day"));//月最后一天
        $dep_id = I('dep_id');
        $name = I('name');
        $map = json_decode(urldecode(I('map')),true);
        if ($map['date']){
            $period_start = $map['date'][1][0];
            $period_end = $map['date'][1][1];
        }
        
        if ($start_date && $end_date) {
            $period_start = $start_date;
            $period_end = $end_date;
        }
        if ($name) $where['name'] = ['like',"%{$name}%"];
        if ($dep_id) $where['dep_id'] = $dep_id;
        

        $where['date'] = ['between',[$period_start,$period_end]];
        $where['duty_on'] = ['neq',''];
        $where['duty_off'] = ['neq',''];
//         $where['_string'] = '(`date_str` not in ("周日","周六") and `overtime` > 0 and duty_off >= concat(`date`," 22:00:00")) or (`date_str` in ("周日","周六") and `overtime` > 480)';
        $where['overtime'] = ['gt',0];
        
        
        
        if ($map) $where = $map;
        $order = 'dep_name asc,name asc,date asc';
        if ($page){
            $listdb = $this->lists($this->model,$where,$order);
        }else{
            $listdb = $this->model->where($where)->order($order)->select();
        }
        foreach ($listdb as $k=>$v){
            $is_work_day = is_work_day($v['date']);
            $sat_duty_off = $v['date'].' 22:00:00';
           //排除1：非工作日并且加班时间少于480 
           //排除2：工作日且加 下班时间是22点之前的
           if ((!$is_work_day && $v['overtime'] < 480) || ($is_work_day && $v['duty_off'] < $sat_duty_off)) {
               unset($listdb[$k]);
           }
        }
        //获取加班数据，条件是overtime>0,(duty_on,duty_off 非空)
        
        $this->assign('period_start',$start_date ? $start_date : $period_start);
        $this->assign('period_end',$end_date ? $end_date :$period_end);
        if ($where) $this->assign('map',urlencode(json_encode($where)));
        return $listdb;
    }
    

    /**
     * 加班报销单,个人分组
     * 
     * date:2017年3月23日
     * author: EK_熊
     */
    public function overtime_form(){
        $listdb = $this->_overtime_data();
        $list = [];
        $sumfee = [];
        $appModel = D('Apply');
        
        foreach ($listdb as $k=>$v){
            $sn = $v['sn'];
                //没有打卡点，查询外出，调整上下班时间点
                $appInfo = $appModel->where(['sn'=>$v['sn'],'type'=>1,"DATE_FORMAT(start_time,'%Y-%m-%d')"=>$v['date']])->find();
                if ($appInfo) {
                    
                    if (empty($v['duty_on']) || $appInfo['start_time'] < $v['duty_on']) {
                        $v['duty_on'] = $appInfo['start_time'];
                    }
                    if (empty($v['duty_off']) || $appInfo['end_time'] > $v['duty_off']) {
                        $v['duty_off'] = $appInfo['end_time'];
                    }
                }
            //车费
            if (!is_work_day($v['duty_on']) && $v['duty_off'] <= $v['date']." 22:00:00") {
                $v['meal_car_fare'] ='-------';
            }else{
                $v['meal_car_fare'] = '';
            }
            $v['meal_fee'] = $this->meal_fee($v['duty_on'], $v['duty_off'],$v['overtime']);
            $v['work_effect'] = !$v['work_effect'] ? '' : round($v['work_effect']/60,2);
            $v['work_actual'] = !$v['work_actual'] ? round($v['overtime']/60,2) : round($v['work_actual']/60,2);
            $v['is_work_day'] = is_work_day($v['duty_on']) ? '工作日' : '非工作日';
            
            $v['duty_on'] = $v['duty_on'] ? $v['duty_on'] : $v['date']." 09:00:00";
            $v['duty_off'] = $v['duty_off'] ? $v['duty_off'] : $v['date']." 18:00:00";
            $list[$sn][] = $v;
            $sumfee[$sn] = $sumfee[$sn]+$v['meal_fee'];
        }
        $attCfg = D('Options')->getOption('att_base',true);
        $this->assign('list',$list);  
        $this->assign('sumfee',$sumfee);
        $this->assign('overtime_charge',$attCfg['overtime_charge']);
        $this->assign('overtime_handin',$attCfg['overtime_handin']);
        $this->show_print(ACTION_NAME,'加班报销单');
        $this->display();
    }
    
    
    
    /**
     * 计算餐费
     * 工作日15，非工作日35，（通宵+6）
     * @param unknown $dutyoff
     * date:2017年3月23日
     * author: EK_熊
     */
    public function meal_fee($duty_on,$duty_off,$overtime){
        $workday = is_work_day($duty_on);
        
        $curday = date('Y-m-d',strtotime($duty_on));
        
        $nextday = date('Y-m-d',strtotime($duty_on)+86400);
        
        $nightlong = false;

        if ($duty_off >= "$nextday 05:59:59"){
            $nightlong = true;
        }
        if ($workday){
            $fee = 15;//工作日
            if ($nightlong) $fee = 21; //工作日+通宵
        }else{
            $fee = 35;//非工作
            if ($nightlong) $fee = 41;//非工作日+通宵
        }
           
        return $fee;
         
    }
    
    
}